<?php
ob_start();
/*
	文件块下载
	更新记录：
		2017-06-01 创建
		2017-08-23 完善块下载逻辑
*/
require '../../vendor/autoload.php';

use utils\PathTool;
use utils\HttpHeader;

$head       = new HttpHeader();
$pathSvr    = $head->param("pathSvr");
$blockPath  = $head->param("blockPath");
$blockIndex = $head->param("blockIndex");
$blockOffset= $head->param("blockOffset");
$blockSize  = $head->param("blockSize");
$blockPath  = PathTool::url_decode($blockPath);

//相关参数不能为空
if (   (strlen($blockIndex)>0 )  
	&& (!empty($blockPath) ) 
	&& (!empty($blockSize)) 
	&& (!empty($pathSvr)) )
{	
	header("Content-Length: $blockSize");
	header("Pragma: No-cache");
	header("Cache-Control: no-cache");
	header("Expires: 0");

	//windows环境需要转成gbk编码,linux环境则不用
	$partPath = PathTool::combin($blockPath, "$blockIndex.part");
	$partPath = iconv("utf-8","gbk",$partPath);
	$len = intval($blockSize);

	$fp = fopen($partPath,"rb");
	fseek($fp, intval($blockOffset));
	while($len > 0)
	{
		set_time_limit(0);
		print(fread($fp,1048576));
		flush();
		ob_flush();
		$len -= 1048576;
	}
	fclose($fp);
}
else
{
	header('HTTP/1.1 500 io error');
	header('Content-Length: ' . ob_get_length());	
}
?>